public class Test {
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int binary(Long x){
        int res = 0;
        int t = 1;
        while (x != 0){
            res += t * (x % 10);
            t *= 2;
            x /= 10;
        }
        return res;
    }
    public int preOrder(TreeNode root, Long t){
        int left = 0, right = 0;
        if (root.left == null && root.right == null){
            System.out.println(t);
            return binary(t);
        }
        if (root.left != null){
            Long next = t*10 + root.left.val;
            left = preOrder(root.left, next);
        }
        if (root.right != null){
            Long next = t*10 + root.right.val;
            right = preOrder(root.right, next);
        }
        int res = left + right;
        return res;
    }
    public int sumRootToLeaf(TreeNode root) {
        Long next = root.val;
        if (root.left == null && root.right == null) {
            return binary(next);
        }
        int res = 0;
        res = preOrder(root,next);
        return res;
    }
}